package com.gjy.util.encryption.sm.sm3;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-07-07 15:37:51
 */
public class Sm3 {

    private static Invocable invocable = null;

    public static String sm3(String msg) {
        if (msg != null && !msg.trim().isEmpty()) {
            String hashData = null;

            try {
                hashData = (String) invocable.invokeFunction("constructSm3", new Object[]{msg});
            } catch (NoSuchMethodException | ScriptException var3) {
                var3.printStackTrace();
            }

            return hashData;
        } else {
            return "";
        }
    }

    static {
        try {
            InputStream inputStream = Sm3.class.getClassLoader().getResourceAsStream("sm/sm3.js");
            ScriptEngine engine = (new ScriptEngineManager()).getEngineByName("nashorn");
            engine.eval(new BufferedReader(new InputStreamReader(inputStream)));
            invocable = (Invocable) engine;
        } catch (ScriptException var2) {
            var2.printStackTrace();
        }

    }

}
